﻿using System;
using Sustainalytics.Caching;

namespace Sustainalytics.Synchronization.BusinessLogic
{
    public class RedisRepository : IRedisRepository
    {
        private AppRedisCache _redisClient;

        private RedisRepository() { }

        public static IRedisRepository GetInstance(AppRedisCache redisClient)
        {
            if (redisClient == null)
            {
                throw new ArgumentException("An instance of type AppRedisCache must be provided to create an RedisRepository instance!");
            }

            return new RedisRepository { _redisClient = redisClient };
        }

        public static IRedisRepository GetInstance(string connectionString, int databaseId)
        {
            if (string.IsNullOrEmpty(connectionString))
                throw new ArgumentException("A connection string must be provided to create an RedisRepository instance!");

            return new RedisRepository { _redisClient = new AppRedisCache(connectionString, databaseId, true) };
        }

        public bool SetClientDWConnectionString(string connectionString)
        {
            const string key = "ClientDWConnectionString";

            _redisClient.Set(key, connectionString);

            var result = _redisClient.GetString(key);

            return !string.IsNullOrEmpty(result);
        }

        public string GetClientDWConnectionString()
        {
            const string key = "ClientDWConnectionString";
            
            var result = _redisClient.GetString(key);

            return result;
        }
    }
}
